<?php
class StopManager extends ORMManager
{
	private $project_id=0;
	
	private $used_projects = array(1,2,3,4,5,6,7); 
	
	function __construct($project_id = 0){
		if (in_array($project_id, $this->used_projects)) $this->project_id = $project_id;
		else $this->project_id = 0;
	}
	
	function GetUsedProjects()
	{
		global $options;
			$qs = 'SELECT projects.project_id,projects.name'
						.' FROM ' 
								.$options['passport']['database'].'.projects AS projects'
			 		 	.' WHERE ';
			 		 		foreach ($this->used_projects as $key=>$value) 
			 		 		{
			 		 			$qs.='projects.project_id='.$value.($key!=(count($this->used_projects)-1)?' OR ':' ');
			 		 		}
		
	//	$ret_arr[0]='- All projects -';
		foreach ($this->getArray($qs) as $key=>$value)
		{
			$ret_arr[$value['project_id']]=$value['name'];	
		}
		
		return $ret_arr;	 			
	}
	
	function checkWordAccess($text)
	{
		global $options;
		$stop_count = 0;
		$qs = 'SELECT stop_word.word_id,stop_word.word FROM '.$options['passport']['database'].'.stop_word AS stop_word'
		 		 			.' LEFT JOIN '.$options['passport']['database'].'.stop_projects AS stop_projects'
		 		 						.' ON stop_word.word_id=stop_projects.word_id';
		 		 			if($this->project_id>0)
		 		 			{
		 		 				$qs.=' WHERE '
		 		 					.'(stop_projects.project_id='.$this->project_id
		 		 					.' AND stop_projects.active=1)'
		 		 					.' OR stop_projects.project_id IS NULL'		
		 		 					;
		 		 			}
		$stop_list = $this->getArray($qs);
		$matches=null;
		$reg_text=strtolower($text);
		
		foreach ($stop_list as $item)
		{
			$reg_mask = strtolower($item['word']);
			
			if (preg_match('%'.$reg_mask.'%i',$reg_text))
			{
				$stop_count++;

				if(!in_array($item['word'],$matches))$matches[] = $item['word'];
				func_log($item['word_id'],'STOP_WORDS_LOG', LOG_ERR);
			}
		}
		$this->matches=$matches;
		return $stop_count;
	}
	
	function checkIPAccess($ip)	{
		global $options;
		$qs = "SELECT * FROM ".$options['passport']['database'].".stop_ip WHERE ip=".$ip;
		$stop_ip = $this->getOneArray($qs);
		if (count($stop_ip)) return true;
		return false;
	}
	
	function getIpID($data)	{
		global $options;
		$qs="SELECT * FROM ".$options['passport']['database'].".ip_list WHERE coockies='".$data."'";
		$ip_id=$this->getOneArray($qs);
		return $ip_id['list_id'];
	}
	
	function insertIpID($data){
		$ip_list = new Ip_listDB();
		$ip_list->ip = REMOTE_ADDR;
		$ip_list->check_not_array=array('date');
		$ip_list->date = 'NOW()';
		$ip_list->coockies = $data;
		$this->insert($ip_list);
		return $ip_list->list_id;
	}
	//ADDon
	function GetWordsWithSpamFromCheckAccess()
	{
		return $this->matches;
	}
	function GetSearchSpamWords($search_param)
	{
		global $options;
		$qs = 'SELECT stop_word.word_id,stop_word.word FROM '
						.$options['passport']['database'].'.stop_word AS stop_word';
			if($this->project_id>0)
			{
					$qs.=','.$options['passport']['database'].'.stop_projects AS stop_projects'
					.' WHERE '
	 					.'stop_projects.word_id=stop_word.word_id'
						.' AND '
	 					.'stop_projects.project_id='.$this->project_id		
	 					.' AND '
						.'stop_word.word LIKE "%'.$search_param.'%"'
		 		 		;
			}
			else
		 	{
		 		$qs.=' WHERE '
		 				.'stop_word.word LIKE "%'.$search_param.'%"'
		 				;
		 	}
		$tmp_arr=$this->getArray($qs);
		foreach ($tmp_arr as $item)
		{
			$ret_arr[$item['word_id']]=$item['word'];
		}
	return $ret_arr;
	}
/**
 * @param $word_id int contaion ID of used word
 * @return array arr[stop_projects_id]=project_id
 */	
	function GetWordUsedProjects($word_id)
	{
		global $options;
		$qs = 'SELECT stop_projects_id,project_id'
					.' FROM '
						.$options['passport']['database'].'.stop_projects'
					.' WHERE '
	 					.'word_id='.$word_id
	 					.' AND '
	 					.'active=1'
	 					;		
		$tmp_arr=$this->getArray($qs);
		foreach ($tmp_arr as $item)
		{
			$ret_arr[$item['project_id']]=$item['project_id'];
		}
		if(count($ret_arr)==0)
		{
			$used_projects_arr=$this->GetUsedProjects();
			foreach ($used_projects_arr as $key=>$val)
			{
				$qs = 'INSERT INTO '
								.$options['passport']['database'].'.stop_projects'
						.' SET '
								.'word_id='.$word_id.','
	 							.'project_id='.$key.','
	 							.'active=1'
								;
				$this->execQuery($qs);
			}
			$qs = 'SELECT stop_projects_id,project_id'
						.' FROM '
							.$options['passport']['database'].'.stop_projects'
						.' WHERE '
		 					.'word_id='.$word_id
		 					.' AND '
		 					.'active=1'
		 					;		
			$tmp_arr=$this->getArray($qs);
			foreach ($tmp_arr as $item)
			{
				$ret_arr[$item['project_id']]=$item['project_id'];
			}
			return $ret_arr;
		}
		else 
		{
			return $ret_arr;			
		}
		 
		 
	}
	function GetStopWordByID($word_id)
	{
		global $options;
		$qs = 'SELECT word'
					.' FROM '
						.$options['passport']['database'].'.stop_word'
					.' WHERE '
	 					.'word_id='.$word_id
	 					;		
	 	
		$ret_arr=$this->getOneArray($qs);
	return $ret_arr['word'];
	}
	function SetProjectForStopWord($word_id,$project_id)
	{
		global $options;
		$qs = 'SELECT stop_projects_id,active'
					.' FROM '
						.$options['passport']['database'].'.stop_projects'
					.' WHERE '
	 						.'word_id='.$word_id
	 					.' AND '
	 						.'project_id='.$project_id
	 					;		
	 	
		$tmp_arr=$this->getArray($qs);
		
		if(count($tmp_arr)==1)
		{
			$tmp_arr_sub=$this->getOneArray($qs);
			if((int)$tmp_arr_sub['active']==1)$ins_val=0;
			else	$ins_val=1;
			print $ins_val;
			$qs = 'UPDATE '
					.$options['passport']['database'].'.stop_projects'
						.' SET '
							.'active='.$ins_val
						.' WHERE '
	 							.'word_id='.$word_id
			 				.' AND '
	 							.'project_id='.$project_id
								;
			$flag=$this->execQuery($qs);
			
		}
		else 
		{
			$qs = 'INSERT INTO '
					.$options['passport']['database'].'.stop_projects'
						.' SET '
	 							.'word_id='.$word_id.','
	 							.'project_id='.$project_id.','
	 							.'active=1'
								;
			$flag=$this->execQuery($qs);
		}
		return $flag;
	}
	function RemoveStopWord($word_id)
	{
		global $options;
		$qs = 'DELETE FROM '
					.$options['passport']['database'].'.stop_word'
			.' WHERE '
	 				.'word_id='.$word_id
			;
		$this->execQuery($qs);
		$qs = 'DELETE FROM '
					.$options['passport']['database'].'.stop_projects'
			.' WHERE '
	 				.'word_id='.$word_id
			;
		return $this->execQuery($qs);
	}
	function AddStopWord($stop_word)
	{
		global $options;
		$qs = 'SELECT word_id'
					.' FROM '
						.$options['passport']['database'].'.stop_word'
					.' WHERE '
	 						.'word LIKE "%'.$stop_word.'%"'
	 				;		
	 	
		$tmp_arr=$this->getArray($qs);
		
		if(count($tmp_arr)==0)
		{	$qs = 'INSERT INTO '
					.$options['passport']['database'].'.stop_word'
				.' SET '
	 				.'word="'.$stop_word.'"'
				;
			$flag=$this->execQuery($qs);
		}
		else $flag=null;
		
		return $flag;
	}
}
?>